package com.ruoyi.project.digital.service.impl;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.project.digital.domain.Constants;
import com.ruoyi.project.digital.domain.StaminaUser;
import com.ruoyi.project.digital.domain.TUser;
import com.ruoyi.project.digital.domain.dto.LoginWechatUser;
import com.ruoyi.project.digital.domain.enums.StaminaTypeEnum;
import com.ruoyi.project.digital.service.IStaminaUserService;
import com.ruoyi.project.digital.service.ITUserService;
import com.ruoyi.project.digital.service.WechatUserTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.digital.mapper.TaskMapper;
import com.ruoyi.project.digital.domain.Task;
import com.ruoyi.project.digital.service.ITaskService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 任务信息表Service业务层处理
 *
 * @author ruoyi
 * @date 2023-10-30
 */
@Service
public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements ITaskService
{
    @Autowired
    private WechatUserTokenService wechatUserTokenService;
    @Autowired
    private ITUserService userService;
    @Autowired
    private IStaminaUserService staminaUserService;


    /**
     * 查询任务信息表
     *
     * @param id 任务信息表ID
     * @return 任务信息表
     */
    @Override
    public Task selectTaskById(Long id)
    {
        return baseMapper.selectTaskById(id);
    }

    /**
     * 查询任务信息表列表
     *
     * @param task 任务信息表
     * @return 任务信息表
     */
    @Override
    public List<Task> selectTaskList(Task task)
    {
        return baseMapper.selectTaskList(task);
    }

    /**
     * 新增任务信息表
     *
     * @param task 任务信息表
     * @return 结果
     */
    @Override
    public int insertTask(Task task)
    {
        return baseMapper.insertTask(task);
    }

    /**
     * 修改任务信息表
     *
     * @param task 任务信息表
     * @return 结果
     */
    @Override
    public int updateTask(Task task)
    {
        return baseMapper.updateTask(task);
    }

    /**
     * 批量删除任务信息表
     *
     * @param ids 需要删除的任务信息表ID
     * @return 结果
     */
    @Override
    public int deleteTaskByIds(Long[] ids)
    {
        return baseMapper.deleteTaskByIds(ids);
    }

    /**
     * 删除任务信息表信息
     *
     * @param id 任务信息表ID
     * @return 结果
     */
    @Override
    public int deleteTaskById(Long id)
    {
        return baseMapper.deleteTaskById(id);
    }

    @Override
    public Task getFirst() {
        QueryWrapper<Task> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("code", Constants.FIRST);
        return getOne(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void getFirstTask(Long id) {
        Task task=getById(id);
        LoginWechatUser loginMember = wechatUserTokenService.getLoginWechatUser(ServletUtils.getRequest());
        TUser user=new TUser();
        user.setId(loginMember.getId());
        user.setStamina(task.getGetValue());
        userService.updateById(user);
        StaminaUser staminaUser=new StaminaUser();
        staminaUser.setGetValue(task.getGetValue());
        staminaUser.setTimes(new Date());
        staminaUser.setTitle(task.getTitle());
        staminaUser.setUserId(loginMember.getId());
        staminaUser.setType(StaminaTypeEnum.IN.getValue());
        staminaUserService.save(staminaUser);

    }
}
